package com.sky.controller.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.sky.context.BaseContext;
import com.sky.result.Result;
import com.sky.service.RecommendationService;
import com.sky.vo.RecommendationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户推荐相关接口
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserRecommendationController {

    @Autowired
    private RecommendationService recommendationService;

    /**
     * 获取用户的个性化推荐
     *
     * @param limit 推荐数量
     * @return 推荐图书列表
     */
    @SaCheckLogin
    @GetMapping("/recommendation")
    public Result<List<RecommendationVO>> getPersonalizedRecommendations(@RequestParam(defaultValue = "24") Integer limit) {
        log.info("获取用户的个性化推荐");
        try {
            Integer userId = Math.toIntExact(BaseContext.getCurrentId());
            List<RecommendationVO> recommendations = recommendationService.getPersonalizedRecommendations(userId, limit);
            return Result.success(recommendations);
        } catch (Exception e) {
            log.error("获取用户推荐失败", e);
            return Result.error("获取推荐失败：" + e.getMessage());
        }
    }

    /**
     * 获取相似图书推荐
     *
     * @param bookId 图书ID
     * @param limit  推荐数量
     * @return 相似图书列表
     */
    @SaCheckLogin
    @GetMapping("/book/similar/{bookId}")
    public Result<List<RecommendationVO>> getSimilarBooks(@PathVariable Integer bookId, @RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取相似图书推荐");
        try {
            List<RecommendationVO> similarBooks = recommendationService.getSimilarBooks(bookId, limit);
            return Result.success(similarBooks);
        } catch (Exception e) {
            log.error("获取相似图书推荐失败", e);
            return Result.error("获取推荐失败：" + e.getMessage());
        }
    }

    /**
     * 获取热门图书推荐
     *
     * @param limit 推荐数量
     * @return 热门图书列表
     */
    @SaCheckLogin
    @GetMapping("/book/popular")
    public Result<List<RecommendationVO>> getPopularBooks(@RequestParam(defaultValue = "30") Integer limit) {
        log.info("获取热门图书推荐");
        try {
            List<RecommendationVO> popularBooks = recommendationService.getPopularBooks(limit);
            return Result.success(popularBooks);
        } catch (Exception e) {
            log.error("获取热门图书推荐失败", e);
            return Result.error("获取推荐失败：" + e.getMessage());
        }
    }

    /**
     * 记录用户行为
     *
     * @param bookId     图书ID
     * @param actionType 行为类型（VIEW/COLLECT/BORROW/RATE）
     * @return 操作结果
     */
    @SaCheckLogin
    @PostMapping("/book/action/{bookId}")
    public Result recordUserAction(@PathVariable Integer bookId, @RequestParam String actionType) {
        log.info("记录用户行为");
        try {
            Integer userId = Math.toIntExact(BaseContext.getCurrentId());
            recommendationService.recordUserAction(userId, bookId, actionType);
            return Result.success();
        } catch (Exception e) {
            log.error("记录用户行为失败", e);
            return Result.error("记录行为失败：" + e.getMessage());
        }
    }
} 